﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataBaseTable;
using MySpace.Data;
using System.Data;
using System.Text;
public partial class Manage_Ad_Lottery_Get : System.Web.UI.Page
{
    private Dictionary<string, winners> dic = new Dictionary<string, winners>();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["id"]))
        {
            this.idhf.Value = Request.QueryString["id"];
        }
        this.bd();
        from_date_tb.Attributes.Add("readonly", "true");
        to_date_tb.Attributes.Add("readonly", "true");

        if (!IsPostBack)
        {
            this.initDDL();

        }
    }
    private void bd()
    {
        LotteryTable lt = new LotteryTable(Convert.ToInt32(this.idhf.Value));
        if (lt.ID == 0)
        {
            //ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('ID非法');</script>");
            Response.Redirect("~/Manage/Ad_Lottery_List.aspx");
        }
        LotteryName_lb.Text = lt.LotteryName;
    }
    private void initDDL()
    {
        for (int i = 1; i <= 100; i++)
        {
            size_ddl.Items.Add(i+"");
        }
    }
    protected void lottery_gv_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToString().Trim() == "Edit")
        {

        }
    }
    protected void get_btn_Click(object sender, EventArgs e)
    {
        DBHandler dbh = GetHandler.Init();
        DataTable dt = new DataTable();
        if (mokuai_ddl.SelectedIndex ==0)
        {
            dbh.CommandText = string.Format("select id,qname as name,qqq+'@qq.com' as Email from WishingWall where DateDiff( 'd',#{0}# , qdate)>0 and DateDiff( 'd',#{1}# , qdate)<0", from_date_tb.Text, to_date_tb.Text);
            dt=dbh.ExecuteDataTable();
        }
        else
        {
            dbh.CommandText = string.Format("select id,Name,Email from hwguestbook where DateDiff( 'd',#{0}# , theDate)>0 and DateDiff( 'd',#{1}# , theDate)<0", from_date_tb.Text, to_date_tb.Text);
            dt = dbh.ExecuteDataTable();
        }
        Random ran = new Random();
        int i=0;
        int j = 0;
        while (i < dt.Rows.Count && j < Convert.ToInt32(size_ddl.SelectedValue))
        {
            i++;
            DataRow dr= dt.Rows[ran.Next(dt.Rows.Count - 1)];
            if (!dic.ContainsKey(dr["Email"].ToString()))
            {
                dic.Add(dr["Email"].ToString(), new winners(dr, mokuai_ddl.SelectedValue, ran.Next(dt.Rows.Count - 1)));
                j++;
            }
        }
        lottery_gv.DataSource = dic.Values;
        lottery_gv.DataBind();
    }
    public class winners
    {
        public winners(DataRow dr,string mk,int seed)
        {
            this.Eamil = dr["Email"].ToString();
            this.uname = dr["name"].ToString();
            this.id = dr["id"].ToString();
            this.mokuai = mk;
            this.checkCode = this.getcode(seed);
        }
        public string id{get;set;}
        public string Eamil { get; set; }
        public string mokuai { get; set; }
        public string checkCode { get; set; }
        public string uname { get; set; }
        private string getcode(int seed)
        {
            //生成兑奖码
            string sb = "";
            string[] c = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
            Random ran = new Random(seed);
            for (int i = 0; i < 6; i++)
            {
                sb+=c[ran.Next(c.Length-1)];
            }
            return sb;
        }

    }
    protected void save_btn_Click(object sender, EventArgs e)
    {
        foreach(winners wr in dic.Values)
        {
            LotteryListTable llt = new LotteryListTable();
            llt.checkCode = wr.checkCode;

        }
    }
}